package com.cis467.mrball;

import java.util.ArrayList;

import com.cis467.mrball.backend.GameManagement;
import com.cis467.mrball.parts.Level;
import com.cis467.mrball.parts.TestLevel;

import android.content.Context;
import android.graphics.Canvas;
import android.util.AttributeSet;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.widget.Toast;

public class LevelSelectView extends SurfaceView implements SurfaceHolder.Callback{

    private Context mContext;
    
    private SurfaceHolder mSurfaceHolder;
    
    // For drawing to screen
    private LevelSelectScreenManager mScreenManager;
    
    private ArrayList<Level> mLevels;
    
    // The level the is being built
    private int mCurrentLevelIndex;
    
    private String mode = "";
    
    public LevelSelectView(Context context, AttributeSet attrs) {
        
    	super(context, attrs);
    	
    	// Save the context
    	mContext = context;
    	
    	mLevels = new ArrayList<Level>();
    	mLevels.add(new TestLevel().getLevel());
        
    	mCurrentLevelIndex = 0;
    	
    	// Create a screen manager
    	mScreenManager = new LevelSelectScreenManager(mContext);
    	
    	// Setup screen
        mSurfaceHolder = getHolder();
        mSurfaceHolder.addCallback(this);
        mSurfaceHolder.setKeepScreenOn(true);
        setFocusable(true);
    }
    
    public boolean handleTouch(int x, int y) {
    	
    	// Check if the user touched the side panel
    	if(mScreenManager.isSideMenuTouch(x)) {
    		// get the button that was touched
    		
    		if(mScreenManager.isPlayButton(y))
    			return true;
    		
    		else if(mScreenManager.isPrevButton(y))
    			prevLevel();
    		
    		else if(mScreenManager.isNextButton(y))
    			nextLevel();
    	}
    	
    	draw();  
    	
    	return false;
    }
    
    private void nextLevel() {
    	
// For getting one level at a time    	
//    	if(mode.equals("Save") && (mCurrentLevelIndex == (mLevels.size()-1)))
//    		try {
//    			mLevels.add(GameManagement.download_Level(mLevels.size()));
//    			
//    		} 
//    		catch(Exception e) {
//    			
//    		}
    	
    	mCurrentLevelIndex = (mCurrentLevelIndex + 1) % mLevels.size();
    }
    
    private void prevLevel() {
    	
    	mCurrentLevelIndex = (mCurrentLevelIndex + mLevels.size() - 1) % mLevels.size();
    }
    
    private void draw() {
    	
    	Canvas canvas = null;
        
    	try {
        	
    		// Lock and get the canvas
            canvas = mSurfaceHolder.lockCanvas(null);
            
            synchronized (mSurfaceHolder) {
                
            	// Draw the screen
                mScreenManager.draw(canvas, mLevels.get(mCurrentLevelIndex), mode);
            }
        } 
    	
    	finally {

    		// Check if canvas was retrieved
            if (canvas != null) {
            	
            	// Unlock and draw it
                mSurfaceHolder.unlockCanvasAndPost(canvas);
            }
        }
    }

    /* Callback invoked when the surface dimensions change. */
    public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
    	
    	// Update the screen dimensions
    	mScreenManager.setDimensions(width, height);
    	
    	// Draw the initial screen
    	draw();    
    }

    /*
     * Callback invoked when the Surface has been created and is ready to be
     * used.
     */
    public void surfaceCreated(SurfaceHolder holder) {
    	// Do Nothing
    }

    /*
     * Callback invoked when the Surface has been destroyed and must no longer
     * be touched. WARNING: after this method returns, the Surface/Canvas must
     * never be touched again!
     */
    public void surfaceDestroyed(SurfaceHolder holder) {
    	// Do Nothing
    }
    
    public Level getLevel() {
    	return mLevels.get(mCurrentLevelIndex);
    }
    
    public void setMode(String mode) {
    	this.mode = mode;
    	
    	try {
    		
    		if(mode.equals("Play"))
    			mLevels = GameManagement.get_playableLevels();
    		else if(mode.equals("Load"))
    			mLevels = GameManagement.get_builderLevels();
    		else if(mode.equals("Save"))
    			mLevels = GameManagement.download_Level();
    		
    	}
    	catch(Exception e) {
    		
    		Toast.makeText(mContext, "Could not find levels.", Toast.LENGTH_SHORT).show();
    		
    		e.printStackTrace();
    		
    		// Create a blank level
    		mLevels.add(new TestLevel().getLevel()); 
    	}
    }
}

